package com.itheima.health.service.impl;

import com.itheima.health.common.MessageConst;
import com.itheima.health.common.RedisConst;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.dao.SetMealDao;
import com.itheima.health.entity.MediclManDto;
import com.itheima.health.entity.Result;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.nio.BufferOverflowException;
import java.util.Date;
import java.util.List;

/**
 * 预约ServiceImpl
 */
@Service
@Slf4j
@SuppressWarnings("all")
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private SetMealDao setMealDao;


    @Override
    @Transactional
    public Order edidcalAppintment(MediclManDto mediclManDto) {
        //预约日期不能为空

        if (mediclManDto.getOrderDate() == null) {
            throw new BusinessRuntimeException("验证码不匹配");
        }
        log.info("日期已经填写");

        //检查验证码是否正确
        String telephone = mediclManDto.getTelephone();
        String validateCode = mediclManDto.getValidateCode();
        String key = "_" + "ORDER" + "_" + telephone;
        String code = (String) redisTemplate.opsForValue().get(key);
        if (!validateCode.equals(code)) {
           throw new BusinessRuntimeException("验证码不匹配");
        }
        log.info("验证码输入正确");
        //调用服务层查询是否是会员,非会员自动注册为会员
        Member member = memberDao.finByTelephone(telephone);
        log.info("member是:" + member);
        if (member == null) {
            member = new Member();
            BeanUtils.copyProperties(mediclManDto, member);
            member.setPhoneNumber(telephone);
            member.setName(member.getName());
            member.setRegTime(new Date());


            memberDao.insert(member);
            log.info("非会员,已自动注册为会员");
        }
            //查询预约表 ,检查是否重复预约,如果发现此会员预约记录的其中一项遇先预约的
            //会员id,预约日期 预约套餐id相同,则判定为重复预约
            List<Order> orderList = orderDao.findAllByMeberId(member.getId());
            log.info("本会员已有预约数量:" + orderList.size());
            if (orderList != null && orderList.size() > 0) {
                for (Order order : orderList) {
                    if (order.getSetmealId().equals(mediclManDto.getSetmealId()) && order.getOrderDate() == mediclManDto.getOrderDate()) {
                        log.info("重复预约");
                        throw new BusinessRuntimeException("验证码不匹配");
                    }
                }
            }
            //查询预约日期表,检查是否可以预约
            Date orderDate = mediclManDto.getOrderDate();
            OrderSetting orderSetting = orderSettingDao.findCountByOrderDate(orderDate);
            //当前日期不能进行预约
            if (orderSetting == null) {
                log.info("当前日期不能进行预约");
                throw new BusinessRuntimeException("验证码不匹配");
            }
            //当前日期已经约满
            if (orderSetting.getReservations() == orderSetting.getNumber()) {
                log.info("当前日期已经约满");
                throw new BusinessRuntimeException("验证码不匹配");
            }

            //当日预约,预约单保存
            Order order = new Order();
            order.setMemberId(member.getId());
            order.setOrderDate(orderDate);
            order.setOrderType(Order.ORDERTYPE_WEIXIN);
            order.setOrderStatus(Order.ORDERSTATUS_NO);
            order.setSetmealId(mediclManDto.getSetmealId());
            orderDao.add(order);


            log.info("可以预约,预约单已保存");
            //当日预约日期已预约人数+1
            orderSetting.setReservations(orderSetting.getReservations() + 1);
            orderSettingDao.editNumberByOrderDate(orderSetting);
            log.info("预约人数修改成功");
           return order;
        }

    @Override
    public Order findById(Integer id) {
        Order order = orderDao.findById(id);
        return order;
    }

}